package com.kingdee.eas.minghua.material.app;

import org.apache.log4j.Logger;
import javax.ejb.*;
import java.rmi.RemoteException;
import com.kingdee.bos.*;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.rule.RuleExecutor;
import com.kingdee.bos.metadata.MetaDataPK;
//import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;
import com.kingdee.bos.framework.ejb.AbstractBizControllerBean;
//import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.bos.service.IServiceContext;

import java.lang.String;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.framework.app.BillBaseControllerBean;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.minghua.jczlwh.mj.SfshEnum;
import com.kingdee.eas.minghua.material.ClzfsqdFactory;
import com.kingdee.eas.minghua.material.ClzfsqdInfo;
import com.kingdee.eas.minghua.material.CylbgsqdDocCollection;
import com.kingdee.eas.minghua.material.CylbgsqdDocFactory;
import com.kingdee.eas.minghua.material.ZhongbiaoCollection;
import com.kingdee.eas.minghua.material.ZhongbiaoEntryCollection;
import com.kingdee.eas.minghua.material.ZhongbiaoEntryFactory;
import com.kingdee.eas.minghua.material.ZhongbiaoEntryInfo;
import com.kingdee.eas.minghua.material.ZhongbiaoFactory;
import com.kingdee.eas.minghua.material.ZhongbiaoInfo;
import com.kingdee.eas.framework.CoreBillBaseCollection;
import com.kingdee.eas.framework.BillBaseCollection;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.framework.ObjectBaseCollection;
import com.kingdee.eas.minghua.material.CylbgsqdDocInfo;

public class CylbgsqdDocControllerBean extends AbstractCylbgsqdDocControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.minghua.material.app.CylbgsqdDocControllerBean");
    
    
    protected boolean _pass(Context ctx, String BosID) throws BOSException {
		boolean flag = false;
		try {
			CylbgsqdDocInfo info = CylbgsqdDocFactory.getLocalInstance(ctx).getCylbgsqdDocInfo(new ObjectUuidPK(BosID));
			if(!info.getDjzt().equals(SfshEnum.yes)){
				info.setDjzt(SfshEnum.yes);
				CylbgsqdDocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(BosID), info);
				EntityViewInfo view2 = new EntityViewInfo();
				FilterInfo filter2 = new FilterInfo();
				filter2.getFilterItems().add(new FilterItemInfo("project.id", info.getXmmc().getId().toString()));
				view2.setFilter(filter2);
				ZhongbiaoCollection offColl = ZhongbiaoFactory.getLocalInstance(ctx).getZhongbiaoCollection(view2);
				if(offColl.size()>0){
					ZhongbiaoInfo offINfo = offColl.get(0);
					String zbID = offINfo.getId().toString();
					EntityViewInfo view1 = new EntityViewInfo();
					FilterInfo filter1 = new FilterInfo();
					filter1.getFilterItems().add(new FilterItemInfo("offerItem", info.getOfferProject().getId().toString()));
					filter1.getFilterItems().add(new FilterItemInfo("parent", zbID));
					view1.setFilter(filter1);
					ZhongbiaoEntryCollection  enColl=ZhongbiaoEntryFactory.getLocalInstance(ctx).getZhongbiaoEntryCollection(view1);
					if(enColl.size()>0){
						ZhongbiaoEntryInfo zbEnInfo = enColl.get(0);
					   SelectorItemCollection sel = new SelectorItemCollection();
					   zbEnInfo.setPriceall(info.getAfterAmount());
					   offINfo.setTotalAmount(offINfo.getTotalAmount().add(info.getAddAmount()));
					   String ss= zbEnInfo.getId().toString();
					   sel.add("priceall");
					   sel.add("totalAmount");
					   ZhongbiaoEntryFactory.getLocalInstance(ctx).updatePartial(zbEnInfo, sel);
					   ZhongbiaoFactory.getLocalInstance(ctx).updatePartial(offINfo, sel);
					   
				  }
				}
				flag = true;
			}
		} catch (EASBizException e) {
			e.printStackTrace();
		}
		return flag;
 
	}

	protected boolean _reject(Context ctx, String BosID) throws BOSException {
		boolean flag = false;
		try {
			CylbgsqdDocInfo info = CylbgsqdDocFactory.getLocalInstance(ctx).getCylbgsqdDocInfo(new ObjectUuidPK(BosID));
			if(info.getDjzt().equals(SfshEnum.yes)){
				info.setDjzt(SfshEnum.save);
				CylbgsqdDocFactory.getLocalInstance(ctx).update(new ObjectUuidPK(BosID), info);
				EntityViewInfo view2 = new EntityViewInfo();
				FilterInfo filter2 = new FilterInfo();
				filter2.getFilterItems().add(new FilterItemInfo("project.id", info.getXmmc().getId().toString()));
				view2.setFilter(filter2);
				ZhongbiaoCollection offColl = ZhongbiaoFactory.getLocalInstance(ctx).getZhongbiaoCollection(view2);
				if(offColl.size()>0){
					ZhongbiaoInfo offINfo = offColl.get(0);
					String zbID = offINfo.getId().toString();
					EntityViewInfo view1 = new EntityViewInfo();
					FilterInfo filter1 = new FilterInfo();
					filter1.getFilterItems().add(new FilterItemInfo("offerItem", info.getOfferProject().getId().toString()));
					filter1.getFilterItems().add(new FilterItemInfo("parent", zbID));
					view1.setFilter(filter1);
					ZhongbiaoEntryCollection  enColl=ZhongbiaoEntryFactory.getLocalInstance(ctx).getZhongbiaoEntryCollection(view1);
					if(enColl.size()>0){
					   ZhongbiaoEntryInfo zbEnInfo = enColl.get(0);
					   SelectorItemCollection sel = new SelectorItemCollection();
					   zbEnInfo.setPriceall(info.getOfferAmount());
					   offINfo.setTotalAmount(offINfo.getTotalAmount().subtract(info.getAddAmount()));
					   String ss= zbEnInfo.getId().toString();
					   sel.add("priceall");
					   sel.add("totalAmount");
					   ZhongbiaoEntryFactory.getLocalInstance(ctx).updatePartial(zbEnInfo, sel);
					   ZhongbiaoFactory.getLocalInstance(ctx).updatePartial(offINfo, sel);
					}
				  }
				flag = true;
			}
		} catch (EASBizException e) {
			e.printStackTrace();
		}
		return flag;
	}
}